package com.nantes.nantourage.utils;

import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.telephony.PhoneStateListener;
import android.telephony.ServiceState;
import android.telephony.TelephonyManager;
import android.util.Log;

import com.nantes.nantourage.app.SPApplication;

public class PhoneDataHelper {
    private static int valueState;

    public static void initStateListener(Context context) {
        TelephonyManager telephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
        PhoneStateListener listener = new PhoneStateListener() {
            @Override
            public void onServiceStateChanged(android.telephony.ServiceState serviceState) {
                super.onServiceStateChanged(serviceState);
                valueState = serviceState.getState();
            };
        };
        telephonyManager.listen(listener, PhoneStateListener.LISTEN_SERVICE_STATE);
    }

    /**
     * Retourne le num�ro IMEI du t�l�phone
     * 
     * @return
     */
    public static String getIMEI() {
        TelephonyManager telephonyManager = (TelephonyManager) SPApplication.getInstance().getSystemService(
                SPApplication.TELEPHONY_SERVICE);
        return telephonyManager.getDeviceId();
    }

    /**
     * Indique si il y a une interface r�seau active et disponible
     * 
     * @return
     */
    public static boolean networkIsAvailable() {
        ConnectivityManager connectivityManager = (ConnectivityManager) SPApplication.getInstance().getSystemService(
                SPApplication.CONNECTIVITY_SERVICE);
        NetworkInfo network = connectivityManager.getActiveNetworkInfo();
        return (network != null && network.isAvailable());
    }

    public static boolean voiceIsAvailable() {
        return (valueState == ServiceState.STATE_IN_SERVICE);
    }

    /**
     * Retourne le num�ro de t�l�phone de la carte SIM
     * 
     * @return
     */
    public static String getNumTel() {
        TelephonyManager tMgr = (TelephonyManager) SPApplication.getInstance().getSystemService(
                Context.TELEPHONY_SERVICE);
        return tMgr.getLine1Number();
    }

    /**
     * Retourne la version de l'application
     * 
     * @return
     */
    public static String getSoftwareVersion() {
        try {
            PackageInfo packageInfo = SPApplication.getInstance().getPackageManager()
                    .getPackageInfo(SPApplication.getInstance().getPackageName(), 0);
            return packageInfo.versionName;
        } catch (PackageManager.NameNotFoundException e) {
            Log.e("Package name not found", e.toString());
        }
        return "X.X.X.X";
    }

    public static boolean isNotificationAvailable() {
        ConnectivityManager connectivityManager = (ConnectivityManager) SPApplication.getInstance().getSystemService(
                SPApplication.CONNECTIVITY_SERVICE);
        return connectivityManager.getBackgroundDataSetting();
    }

}
